﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebIllustrator
{
    public partial class Default : System.Web.UI.Page
    {
            protected void Page_Load(object sender, EventArgs e)
            {

            }
            protected void btnEnviar_Click(object sender, EventArgs e)
            {
                try
                {
                    var fromAddress = new MailAddress("info@tresmapaches.com.ar", "Contacto Web");
                    var toAddress = new MailAddress("info@tresmapaches.com.ar", "Contacto Web");
                    const string fromPassword = "mapache123";
                    const string subject = "Contacto mediante la Web";
                    StringBuilder body = new StringBuilder();

                    body.AppendLine("<b>Nombre:</b> " + this.txtNombre.Text + " <br/>");
                    body.AppendLine("<b>Email:</b> " + this.txtEmail.Text + " <br/>");
                    body.AppendLine("<b>Teléfono:</b> " + this.txtTelefono.Text + " <br/>");
                    body.AppendLine("<b>Mensaje:</b> " + this.txtMensaje.Text + " <br/>");

                    var smtp = new SmtpClient
                    {
                        Host = "mail.tresmapaches.com.ar",
                        //Port = 8889,
                        //EnableSsl = true,
                        //DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                    };
                    using (var message = new MailMessage(fromAddress, toAddress)
                    {
                        Subject = subject,
                        Body = body.ToString(),
                        IsBodyHtml = true
                    })
                    {
                        smtp.Send(message);
                    }

                    this.LimpiarFormulario();
                    //this.lblMensajeOk.Visible = true;

                    //this.ClientScript.RegisterStartupScript(this.GetType(), "jj", "javascript:alert('El mensaje fue enviado con éxito');");
                    //this.Response.Redirect("~/Default.aspx?#!/pageContact");
                    String script = @"<script type='text/javascript'>alert('El mensaje se envió correctamente. Pronto nos comunicaremos con usted.');</script>";

                    this.ClientScript.RegisterStartupScript(typeof(Page), "alerta", script, false);
                }
                catch (Exception ex)
                { }
            }
            protected void btnLimpiar_Click(object sender, EventArgs e)
            {
                try
                {
                    this.LimpiarFormulario();
                }
                catch (Exception ex)
                { }
            }

            private void LimpiarFormulario()
            {
                this.txtEmail.Text = String.Empty;
                this.txtMensaje.Text = String.Empty;
                this.txtNombre.Text = String.Empty;
                this.txtTelefono.Text = String.Empty;
            }
        }
    }
